<html >
<head>
    <title>组织结构管理</title>
    <link rel="stylesheet" type="text/css" href="themes/default/easyui.css" />
    <script type="text/javascript" src="../WF/Scripts/jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="../WF/Scripts/jquery/jquery.easyui.min.js"></script>
    <script src="../WF/Scripts/jquery/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../WF/Scripts/QueryString.js" type="text/javascript"></script>
    <link href="../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <!-- 引入新版本的类库. -->
    <script src="../WF/Scripts/config.js" type="text/javascript"></script>
    <script src="../WF/Comm/Gener.js" type="text/javascript"></script>

    <script src="../WF/Scripts/bootstrap/bootstrap-treeview/src/js/bootstrap-treeview.js" type="text/javascript"></script>
    <link href="../WF/Scripts/bootstrap/bootstrap-treeview/src/css/bootstrap-treeview.css" rel="stylesheet" type="text/css" />
    <script src="../WF/Scripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <link href="../WF/Scripts/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="../WF/Scripts/bootstrap/css/font-awesome.css" rel="stylesheet" type="text/css" />

    <script src="../WF/Scripts/bootstrap/bootstrap-table/src/bootstrap-table.js" type="text/javascript"></script>
    <link href="../WF/Scripts/bootstrap/bootstrap-table/src/bootstrap-table.css" rel="stylesheet" type="text/css" />
    <script src="../WF/Scripts/bootstrap/bootstrap-table/src/locale/bootstrap-table-zh-CN.js" type="text/javascript"></script>
	<script src="../WF/Scripts/bootstrap/BootstrapUIDialog.js" type="text/javaScript"></script>
   <script type="text/javascript">
       var global = window;
       var deptNo = "";
       global.selectedNodes = [];
       global.selectedRows = [];

       //初始化
       $(function () {

           $('#deptInfoDialog').dialog("close");
           $('#deptEmpDialog').dialog("close");
           $('#empInfo').dialog("close");
           $('#connecteEmp').dialog("close");
           $('#empDepts_Panel').dialog("close");
           $("#pageloading").show();

           //加载菜单
           LoadDeptTreeV2();

           //绑定帮助信息.
           var html = " <h3 style='padding-left:5px'>请点击左侧节点显示数据。</h3>";
           $("#Emps").html(html);
       });

       var nodeId = "";
       function LoadDeptTreeV2() {

           var ens = new Entities("BP.GPM.Depts");
           ens.RetrieveAll();

           var pushData = createJsonToTree(ens, "0");
           var jsonTree = pushData;

           $('#appTree').treeview({

               data: jsonTree,         // 数据源
               highlightSelected: true,    //是否高亮选中
               nodeIcon: 'glyphicon',
               emptyIcon: '',    //没有子节点的节点图标
               multiSelect: false,    //多选
               onNodeChecked: function (event, node) {
                   var selectNodes = getChildNodeIdArr(node, true); //获取所有子节点
                   if (selectNodes) { //子节点不为空，则选中所有子节点
                       $('#appTree').treeview('checkNode', [selectNodes, { silent: true}]);
                   }
               },
               onNodeUnchecked: function (event, node) {
                   var selectNodes = getChildNodeIdArr(node, false); //获取所有子节点
                   if (selectNodes) { //子节点不为空，则取消选中所有子节点
                       $('#appTree').treeview('uncheckNode', [selectNodes, { silent: true}]);
                   }
               },
               onNodeSelected: function (event, node) {
                   nodeId = node.nodeId;
                   deptNo = node.id
                   BindEmps(node.id);

                   deptNo = $('#appTree').treeview('getNode', node.nodeId).id;
                   var tree = $('#appTree');

                   if (node.state.expanded) {
                       //处于展开状态则折叠  
                       tree.treeview('collapseNode', node.nodeId);
                   } else {
                       //展开  
                       tree.treeview('expandNode', node.nodeId);
                   }
               }
           });

           $("#pageloading").hide();
       }

       //绑定人员.
       function BindEmps() {

           var dept = new Entity("BP.GPM.Dept", deptNo);

           var html = "";
           html += "<table style='width:95%; margin-bottom:15px;'>";
           html += "<caption ><a href=\"javascript:EditDept('" + dept.No + "')\" >" + dept.Name + " </a> - <a href='javascript:EditEmp()'>新建人员</a> -  <a href='javascript:RefEmps()'>关联兼职</a>  </caption>";
           html += "<tr>";
           html += "<th>序</th>";
           html += "<th>编号</th>";
           html += "<th>名称</th>";
           html += "<th>主部门</th>";
           html += "<th>邮件</th>";
           html += "<th>电话</th>";
           html += "<th>操作</th>";
           html += "<th>位置</th>";
           html += "</tr>";

           var emps = new Entities("BP.GPM.Emps");
           emps.Retrieve("FK_Dept", dept.No, "Idx");


           var empNos = ",";
           for (var i = 0; i < emps.length; i++) {
               var emp = emps[i];
               html += "<tr ondblclick=\"EditEmp('" + emp.No + "')\" >";
               html += "<td class='Idx' >" + i + "</td>";
               html += "<td>" + emp.No + "</td>";
               html += "<td><a href=\"javascript:EditEmp('" + emp.No + "')\">" + emp.Name + "</a></td>";
               html += "<td>" + dept.Name + "</td>";
               html += "<td>" + emp.Email + "</td>";
               html += "<td>" + emp.Tel + "</td>";
               html += "<td><a href=\"javascript:Del('" + emp.No + "')\" ><img src='../WF/Img/Btn/Delete.gif'>删除</a></td>";
               html += "<td><img src='../WF/Img/Btn/Up.GIF' onclick=\"Up('" + emp.No + "')\" alt='上移' />-<img src='../WF/Img/Btn/Down.gif' onclick=\"Down('" + emp.No + "')\" alt='下移' /> </td>";
               html += "</tr>";
               empNos += emp.No + ',';
           }

           var deptEmps = new Entities("BP.GPM.DeptEmps");
           deptEmps.Retrieve("FK_Dept", deptNo);

           var num = emps.length;

           for (var i = 0; i < deptEmps.length; i++) {

               var deptEmp = deptEmps[i];
               if (empNos.indexOf(',' + deptEmp.FK_Emp + ',') != -1)
                   continue;

               var emp = new Entity("BP.GPM.Emp", deptEmp.FK_Emp);

               html += "<tr ondblclick=\"EditEmp('" + emp.No + "')\" >";
               html += "<td class='Idx' >" + num + "</td>";
               html += "<td>" + emp.No + "</td>";
               html += "<td><a href=\"javascript:EditEmp('" + emp.No + "')\">" + emp.Name + "</a></td>";
               html += "<td>兼职</td>";
               html += "<td>" + emp.Email + "</td>";
               html += "<td>" + emp.Tel + "</td>";
               html += "<td><a href=\"javascript:Remove('" + emp.No + "')\" >移除<img src='../WF/Img/Btn/Delete.gif'></a></td>";
               html += "<td><img src='../WF/Img/Btn/Up.GIF' onclick=\"Up('" + emp.No + "')\" alt='上移' />-<img src='../WF/Img/Btn/Down.gif' onclick=\"Down('" + emp.No + "')\" alt='下移' /> </td>";
               html += "</tr>";
               num++;
           }

           html += "</table>";

           $("#Emps").html(html);
       }

       function Up(no) {
           var emp = new Entity("BP.GPM.Emp", no);
           var data = emp.DoMethodReturnString("DoUp");
           BindEmps();
       }
       function Down(no) {

           var emp = new Entity("BP.GPM.Emp", no);
           var data = emp.DoMethodReturnString("DoDown");
           BindEmps();
       }

       function EditDept(no) {
           var url = '../WF/Comm/En.htm?EnName=BP.GPM.Dept&No=' + no;
           OpenBootStrapModal(url, "eudlgframe", "编辑", '800', '500', "icon-edit", true, null, null);
       }

       function EditEmp(no) {

           if (no == undefined) {
               var url = '../WF/Comm/En.htm?EnName=BP.GPM.Emp&FK_Dept=' + deptNo;
               OpenBootStrapModal(url, "eudlgframe", "新增", '800', '500', "icon-edit", true, null, null);
               return;
           }

           var url = '../WF/Comm/En.htm?EnName=BP.GPM.Emp&No=' + no;
           OpenBootStrapModal(url, "eudlgframe", "编辑", '800', '500', "icon-edit", true, null, null);
       }

       function RefEmps() {

           var url = "../WF/Comm/RefFunc/BranchesAndLeaf.htm?EnName=BP.GPM.Dept&Dot2DotEnsName=BP.GPM.DeptEmps&Dot2DotEnName=BP.GPM.DeptEmp&AttrOfOneInMM=FK_Dept&AttrOfMInMM=FK_Emp&EnsOfM=BP.Port.Emps&DefaultGroupAttrKey=FK_Dept&No=" + deptNo + "&PKVal=" + deptNo + "&s=0.13113811473063341";
           OpenBootStrapModal(url, "eudlgframe", "关联人员", '1000', '500', "icon-edit", true, null, null);
       }

       function Del(no) {

           if (confirm('您确定要删除该人员吗？') == false)
               return;

           //取消人员部门管理关系
           var en = new Entity("BP.GPM.DeptEmp");
           en.SetPKVal(deptNo + "_" + no);
           en.Delete();

           var en = new Entity("BP.GPM.Emp", no);
           en.Delete();

           BindEmps(deptNo);
       }

       function Remove(no) {

           if (confirm('您确定要从该部门中移除吗？') == false)
               return;

           var en = new Entity("BP.GPM.DeptEmp");
           en.SetPKVal(deptNo + "_" + no);
           en.Delete();

           BindEmps();
       }

       //树形结构转换
       var treeJson = [];
       function createJsonToTree(jsonArray, parentNo) {
           for (var i = 0; i < jsonArray.length; i++) {
               var data = jsonArray[i];
               if (data.ParentNo == parentNo) {
                   var checked = false;
                   var selectedNodes = window.selectedNodes;
                   var sel = $.grep(selectedNodes, function (obj) {
                       return data.No == obj.No;
                   });
                   if (sel.length > 0) {
                       checked = true;
                   }
                   treeJson.push({
                       "id": data.No,
                       "text": data.Name,
                       "nodes": creatTreeChildern(jsonArray, data.No),
                       "state": { checked: checked, disabled: false }
                   });
               }
           }
           function _(treeArray) {
               $.each(treeArray, function (i, o) {
                   if ($.isArray(o.nodes)) {
                       if (o.nodes.length == 0) {
                           o.nodes = undefined;
                       } else {
                           _(o.nodes);
                       }
                   }
               });
           }
           _(treeJson);
           return treeJson;
       }

       function creatTreeChildern(jsonArray, parentNo) {
           var childList = []
           for (var i = 0; i < jsonArray.length; i++) {
               var data = jsonArray[i];
               if (data.ParentNo == parentNo) {
                   var checked = false;

                   childList.push({
                       "id": data.No,
                       "text": data.Name,
                       "nodes": creatTreeChildern(jsonArray, data.No),
                       "state": { checked: checked, disabled: false }
                   });
               }
           }

           return childList;
       }
       function getChildNodeIdArr(node, checked) {
           var ts = [];
           if (node.nodes) {
               for (x in node.nodes) {
                   ts.push(node.nodes[x].nodeId);
                   if (node.nodes[x].nodes) {
                       var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);
                       for (j in getNodeDieDai) {
                           ts.push(getNodeDieDai[j]);
                       }
                   }
               }
           } else {
               ts.push(node.nodeId);
           }
           return ts;
       }
   </script>
    <style type="text/css">
    .tableLabel
    {
        text-align:right;
        width:80px;
    }
    #emptt input
    {
        width:200px;
    }
    </style>
</head>
<body class="easyui-layout">
    <div id="pageloading">
    </div>
    <div data-options="region:'west',split:true" style="width: 350px; padding: 1px; overflow: hidden;">
        <div style="width: 100%; height: 95%; overflow: auto;">
            <ul id="appTree" class="easyui-tree-line" data-options="animate:false,dnd:false">
            </ul>
        </div>
    </div>
     
      
   <div data-options="region:'center',split:true" style="">

    <div id="Emps"></div>

    </div>

          
    
</body>
</html>
